/*
 * cdn_power_switch_reset.c
 *
 *  Created on: 2025年4月11日
 *      Author: JunJun
 */

#include "cdn_power_switch_reset.h"
#include "cdn_nvm3.h"
#include "cdn_light.h"
#include "cdn_pwm.h"
#include "sl_pwm.h"

static uint8_t secondNum5S_flag = 0;
static uint8_t secondNum500MS_flag = 0;

static Count ResetCount;

POWER_SWITCH_CMD PowerSwitchCMD;

//获取灯效模式
POWER_SWITCH_MODE Get_CDN_Power_Switch_mode(void)
{
  return PowerSwitchCMD.mode;
}
//获取灯效次数
uint8_t Get_CDN_Power_Switch_time(void)
{
  return PowerSwitchCMD.time;
}

void SetsecondNum5S(uint8_t flag)
{
  secondNum5S_flag = flag;
}

uint8_t GetsecondNum5S(void)
{
  return secondNum5S_flag;
}

void SetsecondNum500MS(uint8_t flag)
{
  secondNum500MS_flag = flag;
}

uint8_t GetsecondNum500MS(void)
{
  return secondNum500MS_flag;
}

//10ms调用
void PowerSwitchHandle(void)
{
  static uint16_t PowerSwitchCount = 0;

  if(!GetsecondNum5S()) //模组上电，未到5S时间,计数开始
    {
      PowerSwitchCount++;
      if(PowerSwitchCount == POWERSWITCHTIME1)//到了500ms时间，开关次数+1，并存入Flash
        {
          cdn_nvm3_read(POWER_SWITCH_RESET_ID62,(char *)(&ResetCount));
          ResetCount.ResetCountNum ++;
          TRACE("lj: ResetCount = 0x%02x\n",ResetCount.ResetCountNum);
          cdn_nvm3_delete(POWER_SWITCH_RESET_ID62);
          cdn_nvm3_write(POWER_SWITCH_RESET_ID62,(uint8_t *)(&ResetCount),sizeof(ResetCount));

          if((ResetCount.ResetCountNum >= POWERSWITCHNUMBER1)&&(ResetCount.ResetCountNum <= POWERSWITCHNUMBER2))//计数次数在7-12次内,执行灯具闪烁,以提示5s后执行复位
            {
              SetLedState(CDN_LED_NONE_STATE,0,0);//此LED状态为筒灯控制状态，受筒灯记录的当前亮度值及色温值影响，设为CDN_LED_NONE_STATE---暂时取消筒灯状态控制
              Set_CDN_Power_Switch_mode(CDN_POWER_SWITCH_BLINK,4);//闪烁操作：1秒亮灭一次
              TRACE("CDN_POWER_SWITCH_BLINK!!\r\n");
            }

          if(ResetCount.ResetCountNum >= 0xFF)
            {
              ResetCount.ResetCountNum = 0;
            }
        }

      if(PowerSwitchCount > POWERSWITCHTIME2)//到了5s时间
        {
          if((ResetCount.ResetCountNum >= POWERSWITCHNUMBER1)&&(ResetCount.ResetCountNum <= POWERSWITCHNUMBER2))//计数次数在7-12次内，执行灯具呼吸以后恢复出厂设置
            {
              SetLedState(CDN_LED_NONE_STATE,0,0);
              Set_CDN_Power_Switch_mode(CDN_POWER_SWITCH_BREATH,2);//呼吸及复位操作：2秒呼吸一次
              TRACE("CDN_POWER_SWITCH_BREATH!!\r\n");
            }

          SetsecondNum5S(1);//已到5秒标志，执行完开关次数清零以后，再也不进入此处

          TRACE("5S is over!!\r\n");

          //将开关次数清零
          ResetCount.ResetCountNum = 0;
          cdn_nvm3_delete(POWER_SWITCH_RESET_ID62);
          cdn_nvm3_write(POWER_SWITCH_RESET_ID62,(uint8_t *)(&ResetCount),sizeof(ResetCount));
        }
    }
  else
    {
      PowerSwitchCount = 0;
    }
}

//设置开关复位灯效模式
void Set_CDN_Power_Switch_mode(POWER_SWITCH_MODE mode, uint8_t time)
{
  static uint8_t prepowermode = 0;
  static uint8_t prepowertime = 0;

  if(prepowermode == mode && prepowertime == time)
  {
    return;
  }

  PowerSwitchCMD.mode = mode;
  PowerSwitchCMD.time = time;

  prepowermode = mode;
  prepowertime = time;
}


//灯具开关复位呼吸灯效,10ms
void CDN_Power_Switch_display(void)
{
  switch(Get_CDN_Power_Switch_mode())
  {
    case CDN_POWER_SWITCH_BREATH:
    {
      CDN_Power_Switch_Breath_Handle();
      break;
    }
    case CDN_POWER_SWITCH_BLINK:
    {
      CDN_Power_Switch_Blink_Handle();
      break;
    }
    default:
      break;
  }
}

void CDN_Power_Switch_Breath_Handle(void)
{
  static uint16_t SwitchBreathCnt = 0;
  static uint8_t SwitchBreathLightness = 0;
  static uint8_t SwitchBreathCmd = BREATH_UP;
  static uint8_t SwitchBreathTime = 0;

  SwitchBreathCnt ++;
  if(SwitchBreathCnt < (POWERSWITCHBREATHPERIOD>>1))
  {
      SwitchBreathCmd = BREATH_UP;
  }
  else if(SwitchBreathCnt >= (POWERSWITCHBREATHPERIOD>>1) && SwitchBreathCnt < POWERSWITCHBREATHPERIOD)
  {
      SwitchBreathCmd = BREATH_DOWN;
  }
  else
  {
      SwitchBreathCmd = BREATH_OVER;
      SwitchBreathCnt = 0;
      SwitchBreathTime ++;
    if(SwitchBreathTime == PowerSwitchCMD.time)
    {
        Set_CDN_Power_Switch_mode(CDN_POWER_SWITCH_NONE, 0);
        SetLedState(CDN_DIM_CT_LED_ON,GetCurrentBrightness(),GetCurrentCTvalue());
        TRACE("Switch_Breath  ------------BREATH_OVER");
        SwitchBreathTime = 0;
        sl_btmesh_initiate_node_reset();
    }
  }

  if(BREATH_UP == SwitchBreathCmd)
  {
      SwitchBreathLightness ++;
      if(SwitchBreathLightness >= PWM_MAX_DUTY_CYCLE)
      {
          SwitchBreathLightness = PWM_MAX_DUTY_CYCLE;
      }
#if CDN_LIGHT_BULB
      GeneralLightnessCtrl(SwitchBreathLightness);
#endif
#if CDN_LIGHT_STRIP
      CdnLightStripCtrlRepeat(SwitchBreathLightness,GetCurrentCTvalue());
#endif
  }
  else if(SwitchBreathCmd == BREATH_DOWN)
  {
      if(SwitchBreathLightness > PWM_MIN_DUTY_CYCLE)
      {
          SwitchBreathLightness --;
      }
#if CDN_LIGHT_BULB
      GeneralLightnessCtrl(SwitchBreathLightness);
#endif
#if CDN_LIGHT_STRIP
      CdnLightStripCtrlRepeat(SwitchBreathLightness,GetCurrentCTvalue());
#endif
  }
}

void CDN_Power_Switch_Blink_Handle(void)
{
  static uint16_t SwitchBlinkCnt = 0;
//  static uint8_t SwitchBlinkLightness = 0;
  static uint8_t SwitchBlinkCmd = BREATH_UP;
  static uint8_t SwitchBlinkTime = 0;

  SwitchBlinkCnt ++;
  if(SwitchBlinkCnt < (POWERSWITCHBLINKPERIOD>>1))
  {
      SwitchBlinkCmd = BREATH_UP;
  }
  else if(SwitchBlinkCnt >= (POWERSWITCHBLINKPERIOD>>1) && SwitchBlinkCnt < POWERSWITCHBLINKPERIOD)
  {
      SwitchBlinkCmd = BREATH_DOWN;
  }
  else
  {
      SwitchBlinkCmd = BREATH_OVER;
      SwitchBlinkCnt = 0;
      SwitchBlinkTime ++;
    if(SwitchBlinkTime == PowerSwitchCMD.time)
    {
        TRACE("Switch_Blink  ------------BREATH_OVER");
        Set_CDN_Power_Switch_mode(CDN_POWER_SWITCH_NONE, 0);
        SetLedState(CDN_DIM_CT_LED_ON,GetCurrentBrightness(),GetCurrentCTvalue());
        SwitchBlinkTime = 0;
    }
  }

  if(BREATH_UP == SwitchBlinkCmd)
  {
//      SwitchBlinkLightness = PWM_MAX_DUTY_CYCLE;
      GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
//#if CDN_LIGHT_BULB
//      GeneralLightnessCtrl(SwitchBlinkLightness);
//#endif
//#if CDN_LIGHT_STRIP
//      CdnLightStripCtrlRepeat(SwitchBlinkLightness,GetCurrentCTvalue());
//#endif
  }
  else if(SwitchBlinkCmd == BREATH_DOWN)
  {
//      SwitchBlinkLightness = PWM_MIN_DUTY_CYCLE;
      GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 0);
//#if CDN_LIGHT_BULB
//      GeneralLightnessCtrl(SwitchBlinkLightness);
//#endif
//#if CDN_LIGHT_STRIP
//      CdnLightStripCtrlRepeat(SwitchBlinkLightness,GetCurrentCTvalue());
//#endif
  }
}
